package com.shqcjd.preinstallmobile.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.model.pojo.EquipmentGroupBean;
import com.shqcjd.preinstallmobile.ui.activity.EquipmentAbnormalActivity;
import com.shqcjd.preinstallmobile.ui.activity.EquipmentGroupActivity;

import java.util.List;

public class EquipmentGroupItemAdapter extends BaseQuickAdapter<EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX,EquipmentGroupItemAdapter.ViewHolder> {
    private EquipmentGroupActivity context;
    List<EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX> data;
    private int epId, tId;
    public EquipmentGroupItemAdapter(@Nullable List<EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX> data, EquipmentGroupActivity context, int epId, int tId) {
        super(R.layout.item_equipment_group_child, data);
        this.context = context;
        this.data = data;
        this.epId = epId;
        this.tId = tId;
    }


    @Override
    protected void convert(ViewHolder helper, EquipmentGroupBean.EquipmentCheckListBean.EquipmentChildrenBeanX item) {
        helper.tv_item_equiment_group_name.setText(item.equipment_name);
        helper.cb_item_equipment_group_switch.setChecked(data.get(helper.getAdapterPosition()).running);
        if(item.check_normal==1){
            helper.rb_item_equipment_group_confirm.setChecked(true);
        }else if(item.check_normal==2){
            helper.rb_item_equipment_group_cancle.setChecked(true);
        }

        helper.rv_item_equipment_list.setLayoutManager(new LinearLayoutManager(context));
        EquipmentGroupItemChildAdapter equipmentGroupItemChildAdapter = new EquipmentGroupItemChildAdapter(item.equipment_children, context, item.equipment_id, tId);
        helper.rv_item_equipment_list.setAdapter(equipmentGroupItemChildAdapter);

        helper.cb_item_equipment_group_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    equipmentGroupItemChildAdapter.setCheckTrue();
                }else {

                    equipmentGroupItemChildAdapter.setCheckFalse();
                }
            }
        });

        helper.cb_item_equipment_group_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.openOrCloseSwitchHttp(item.kks);
            }
        });
        context.radioGroupList.add(helper.rg_item_equipment_group);
        helper.rg_item_equipment_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb_item_equipment_group_cancle){
                    Intent it = new Intent(context, EquipmentAbnormalActivity.class);
                    it.putExtra("eId", item.equipment_id);
                    it.putExtra("tId", tId);
                    context.startActivityForResult(it, context.END_CODE);
                }else if(checkedId==R.id.rb_item_equipment_group_confirm){
                    context.updateTrue(item.equipment_id);
                }
            }
        });
    }

     class ViewHolder extends BaseViewHolder {
        private RecyclerView rv_item_equipment_list;
        private TextView tv_item_equiment_group_name;
        private RadioButton rb_item_equipment_group_confirm, rb_item_equipment_group_cancle;
        private CheckBox cb_item_equipment_group_switch;
        private RadioGroup rg_item_equipment_group;

         ViewHolder(View itemView) {
            super(itemView);
             rb_item_equipment_group_confirm = itemView.findViewById(R.id.rb_item_equipment_group_confirm);
             rb_item_equipment_group_cancle = itemView.findViewById(R.id.rb_item_equipment_group_cancle);
             tv_item_equiment_group_name = itemView.findViewById(R.id.tv_item_equiment_group_name);
             rv_item_equipment_list = itemView.findViewById(R.id.rv_item_equipment_list);
             cb_item_equipment_group_switch = itemView.findViewById(R.id.cb_item_equipment_group_switch);
             rg_item_equipment_group = itemView.findViewById(R.id.rg_item_equipment_group);
        }
    }

    public void setCheckTrue(){
        for (int i=0; i<data.size(); i++){
            data.get(i).running = true;
        }
        notifyDataSetChanged();
    }

    public void setCheckFalse(){
        for (int i=0; i<data.size(); i++){
            data.get(i).running = false;
        }
        notifyDataSetChanged();
    }
}
